import { defineStore } from 'pinia';
import { isEmpty, cloneDeep, clone } from 'lodash';
import loginApi from '@/sheep/api/login'

// 默认用户信息
const defaultUserInfo = {
  totalRevenue:0, // 累计收益
  nickname:'',
  phone:'',
  accountType:'',
  szAgencyName:'',//是否开启数字人直播 1开启 0未开启
  szEnergy:'0',// 数字人直播 可用能量值
  szWalletBalance:'0',// 数字人直播 钱包余额
  todayAmount:'0',// 数字人直播 今日钱包余额收益
  szWithdrawalBalance:'0',// 数字人直播 可提现钱包余额
  openingTime: 0,  // 数字直播开通天数
  invitationCode:'',
  avatar: "http://img.menghu.cloud/mall/header.png",  // 头像地址
};


const user = defineStore({
  id: 'user',
  state: () => ({
    userInfo: '', // 用户信息
	bankInfo:'',//银行卡信息
    isLogin: uni.getStorageSync('token'), // 登录状态
	user:''
  }),
  getters: {},
 actions: {
    getInfo(userInfo = '') {
		
		if(!!uni.getStorageSync('token')){
			this.getUserInfo()
		}else{
			this.userInfo=clone(defaultUserInfo)
		}
   },
   
   getUserInfo(){
	  loginApi.userInfo().then(res=>{
		  console.log('res','11111');
		  
		  this.userInfo=res.data.userInfo
		  this.bankInfo=res.data.bankInfo
	  })
   },
   //登出
   logout(force = false) {
         if (!force) {
            console.log('12341234');
			 this.userInfo=clone(defaultUserInfo)
			  uni.removeStorageSync('token')
         }
       
         
       },
	
	//登录
	login(item){
		const _this=this
		loginApi.loginApi(item).then(res=>{
			if(res.code==200){
				  uni.showToast({
					title:"切换成功！",
					icon:"none"
				  })
				  //存储token
				  uni.setStorageSync('token',res.data)
				  
				  
				 setTimeout(()=>{
					 uni.switchTab({
						  url:'/pages/index/index'
					 })
					 _this.getUserInfo()
				 },300)
			}
		})
	}
  },
  persist: {
    enabled: true,
    strategies: [
      {
        key: 'user-store',
      },
    ],
  },
  
});

export default user;
